package cn.edu.hust.youth.tzb.servlet.student;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.hust.youth.tzb.dao.TeamDAO;
import cn.edu.hust.youth.tzb.model.Student;
import cn.edu.hust.youth.tzb.service.StudentException;

public class Addteam extends HttpServlet {
	private static final String CONTENT_TYPE = "text/html; charset=GBK";
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType(CONTENT_TYPE);
		Student s=(Student)request.getSession().getAttribute("STUDENT");
		String teamname=request.getParameter("teamname");
		TeamDAO td=new TeamDAO();
		try {
			String target = "/student/viewteam.jsp";
			td.addTeam(teamname, s.getUsername());
			String teamid=td.getTeamidFromLeader(s.getUsername());
			td.setTeamLeader(s.getId(),teamid);
			request.setAttribute("error", "0");
            RequestDispatcher rd = this.getServletContext().getRequestDispatcher(target);
            rd.forward(request,response);
		} catch (StudentException e) {
			e.printStackTrace();
			request.setAttribute("error", "1");
		}
		catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("error", "1");
		}
		
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
	
}
